
package com.yuan.graduate;

import com.yuan.adapt.CommDetailAdapt;
import com.yuan.application.BaseActivity;
import com.yuan.prefence.FileConstants;
import com.yuan.unit.AsyncDataJobHandler;
import com.yuan.unit.FileUtils;
import com.yuan.unit.TimeUtils;
import com.yuan.unit.ToastHelper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查看考勤界面
 * 
 * @author YUAN
 */
@SuppressWarnings({
        "rawtypes", "unchecked", "unused"
})
@SuppressLint("HandlerLeak")
public class CheckActivity extends BaseActivity implements OnItemClickListener {
    private Context mContext = null;
    private Handler mHandler = null;
    private ListView exlv_check;
    private DatePicker dp_check = null;
    private List daylist = new ArrayList();
    private CommDetailAdapt dataAdapter;
    private TextView dp_tv;
    private int CheckedCount = -1, WorkDays = -1;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_check);
        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what)
                {
                    case GETMONTHCHECKSUCCEED:
                        updateListData();
                        break;
                    case GETMONTHCHECKFILLED:
                        break;
                }
            }

        };

        initView();
        initDataTime();
    }

    /**
     * 初始化十日组件
     */
    private void initDataTime() {
        // TODO Auto-generated method stub
        Calendar ca = Calendar.getInstance();
        int year = ca.get(Calendar.YEAR);
        int month = ca.get(Calendar.MONTH);
        int day = ca.get(Calendar.DATE);
        updateList(year, month + 1);
        dp_check.init(year, month, day, new OnDateChangedListener() {
            @Override
            // 实现该接口 当用户点击改变日期是会被调用
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                // TODO Auto-generated method stub
                updateList(year, monthOfYear + 1);
            }
        });
    }

    private void initView() {
        // TODO Auto-generated method stub
        dp_check = (DatePicker) findViewById(R.id.dp_check);
        if (dp_check != null) {
            ((ViewGroup) ((ViewGroup) dp_check.getChildAt(0)).getChildAt(0)).getChildAt(1)
                    .setVisibility(View.GONE);
        }
        exlv_check = (ListView) findViewById(R.id.exlv_check);
        dataAdapter = new CommDetailAdapt(mContext, daylist, "show", "mark");
        exlv_check.setAdapter(dataAdapter);
        exlv_check.setOnItemClickListener(this);
        dp_tv = (TextView) findViewById(R.id.dp_tv);
    }

    private void updateList(int year, int month) {
        // TODO Auto-generated method stub
        daylist.clear();
        int dayS = TimeUtils.getMonthLastDay(year, month);
        Map map = null;
        for (int i = 0; i < dayS; i++)
        {
            map = new HashMap();
            if (month < 10)
            {
                if (i < 10)
                {
                    map.put("data", year + "-" + "0" + month + "-0" + (i + 1) + "");
                    map.put("show", year + "年" + "0" + month + "月0" + (i + 1) + "日");

                } else
                {
                    map.put("data", year + "-" + "0" + month + "-" + (i + 1) + "");
                    map.put("show", year + "年" + "0" + month + "月" + (i + 1) + "日");
                }

            } else {
                if (i < 10)
                {
                    map.put("data", year + "-" + month + "-0" + (i + 1) + "");
                    map.put("show", year + "年" + month + "月0" + (i + 1) + "日");

                } else
                {
                    map.put("data", year + "-" + "0" + month + "-" + (i + 1) + "");
                    map.put("show", year + "年" + "0" + month + "月" + (i + 1) + "日");
                }
            }
            daylist.add(map);
        }
        if (month < 10)
        {
            AsyncDataJobHandler.getInstance().getmonthcheck(mHandler, year + "-" + "0" + month);
        }
        else
        {
            AsyncDataJobHandler.getInstance().getmonthcheck(mHandler, year + "-" + month);
        }
    }

    private void updateListData() {
        // TODO Auto-generated method stub
        ((BaseAdapter) dataAdapter).notifyDataSetChanged();
        List temp = null;
        try {
            temp = (List) FileUtils.File2erializable(FileConstants.SERIALIZABLE_月考勤记录);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (int i = 0; i < daylist.size(); i++)
        {
            boolean flag = false;
            for (int j = 0; j < temp.size(); j++)
            {
                if (((Map) daylist.get(i)).get("data").equals(((Map) temp.get(j)).get("data")))
                {
                    flag = true;
                }
            }
            if (flag) {
                ((Map) daylist.get(i)).put("mark", "存在考勤记录");
            } else
            {
                ((Map) daylist.get(i)).put("mark", "不存在考勤记录");
            }
        }
        exlv_check.invalidate();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
        if (((Map) daylist.get(position)).get("mark").equals("存在考勤记录")) {
            Intent mIntent = new Intent(mContext, CheckDetailActivity.class);
            mIntent.putExtra("date", (String) ((Map) daylist.get(position)).get("data"));
            startActivity(mIntent);
        } else
        {
            ToastHelper.getInstance().shortToast(mContext, "不存在考勤记录");
        }

    }
}
